package com.yuju.controller.hotel;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.hotel.enums.RoomRepairReturnCodeEnum;
import com.yuju.hotel.model.RoomRepairModel;
import com.yuju.hotel.service.IRoomRepairService;
import com.yuju.log.service.IRoomLogService;
import com.yuju.util.Utils;
import com.yuju.vo.hotel.RoomRepairVO;

/**
 * 
 * 类名：RoomRepairController  <br />
 *
 * 功能：房间维修信息
 *
 * @author : zhousc <br />
 * @Date : 2017年12月15日 上午9:57:01  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("roomRepair")
public class RoomRepairController extends BaseController<RoomRepairVO, RoomRepairModel> {

	@Autowired
	private IRoomRepairService roomRepairService;
	
	/**
	 * 
	 * 功能：列表<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月15日 上午9:56:50 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj) {
		RoomRepairVO  roomRepairVO = obj.toJavaObject(RoomRepairVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roomRepairVO, "pager", true);
		if (!fail.isEmpty()) {
				return validateError(fail);
			}
		Result result = new Result();
		PageModel pageModel = roomRepairService.pageList(convert(roomRepairVO), roomRepairVO.getPage(), roomRepairVO.getPageSize());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomRepairReturnCodeEnum.ROOMREPAIR_QUERY_SUCCESS.getCode());
			result.setReturnMsg(RoomRepairReturnCodeEnum.ROOMREPAIR_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	
	/**
	 * 
	 * 功能：保存<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月15日 上午9:57:52 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj) {
		RoomRepairVO  roomRepairVO = obj.toJavaObject(RoomRepairVO.class);
		Map<String, String> fail = FieldValidator.validateAll(roomRepairVO, "save", true);
		if (!fail.isEmpty()) {
				return validateError(fail);
			}
		Result result = new Result();
		RoomRepairModel model = convert(roomRepairVO);
		int rows = 0;
		for(int i = 0 ;i < model.getRoomIds().length ; i++){
			rows = 0;
			model.setRoomId(model.getRoomIds()[i]);
			rows = roomRepairService.save(model);
		}
		
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomRepairReturnCodeEnum.ROOMREPAIR_SAVE_SUCCESS.getCode());
			result.setReturnMsg(RoomRepairReturnCodeEnum.ROOMREPAIR_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomRepairReturnCodeEnum.ROOMREPAIR_SAVE_FAILURE.getCode());
			result.setReturnMsg(RoomRepairReturnCodeEnum.ROOMREPAIR_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：更新<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月15日 上午9:59:26 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj) {
		RoomRepairVO  roomRepairVO = obj.toJavaObject(RoomRepairVO.class);
		Map<String, String> fail = FieldValidator.validateAll(roomRepairVO, "update", true);
		if (!fail.isEmpty()) {
				return validateError(fail);
			}
		Result result = new Result();
		int rows = roomRepairService.updateSelective(convert(roomRepairVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomRepairReturnCodeEnum.ROOMREPAIR_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(RoomRepairReturnCodeEnum.ROOMREPAIR_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomRepairReturnCodeEnum.ROOMREPAIR_UPDATE_FAILURE.getCode());
			result.setReturnMsg(RoomRepairReturnCodeEnum.ROOMREPAIR_UPDATE_FAILURE.getDesc());
		}
		return result;
	}


	/**
	 * 
	 * 功能：删除<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月15日 上午9:59:18 <br/>
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject obj) {
		RoomRepairVO  roomRepairVO = obj.toJavaObject(RoomRepairVO.class);
		Map<String, String> fail = FieldValidator.validateAll(roomRepairVO, "del", true);
		if (!fail.isEmpty()) {
				return validateError(fail);
			}
		Result result = new Result();
		int rows = roomRepairService.del(roomRepairVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomRepairReturnCodeEnum.ROOMREPAIR_DEL_SUCCESS.getCode());
			result.setReturnMsg(RoomRepairReturnCodeEnum.ROOMREPAIR_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomRepairReturnCodeEnum.ROOMREPAIR_DEL_FAILURE.getCode());
			result.setReturnMsg(RoomRepairReturnCodeEnum.ROOMREPAIR_DEL_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：根据房间ID获取维修信息<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月15日 上午9:58:53 <br/>
	 */
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj) {
		RoomRepairVO  roomRepairVO = obj.toJavaObject(RoomRepairVO.class);
		Map<String, String> fail = FieldValidator.validateAll(roomRepairVO, "get", true);
		if (!fail.isEmpty()) {
				return validateError(fail);
			}
		Result result = new Result();
		RoomRepairModel model = roomRepairService.findById(roomRepairVO.getId());
		if (Utils.isNotNull(model)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(model);
			result.setReturnCode(RoomRepairReturnCodeEnum.ROOMREPAIR_QUERY_SUCCESS.getCode());
			result.setReturnMsg(RoomRepairReturnCodeEnum.ROOMREPAIR_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：完成维修 操作接口<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月16日 下午1:13:04 <br/>
	 */
	@RequestMapping("finishRepair")
	@ResponseBody
	public Result finishRepair(@RequestBody JSONObject obj) {
		RoomRepairVO  roomRepairVO = obj.toJavaObject(RoomRepairVO.class);
		Result result = new Result();
		RoomRepairModel model = convert(roomRepairVO);
		int finishRepair = 0;
		for(int i = 0 ;i < model.getRoomIds().length ; i++){
			finishRepair = 0;
			model.setRoomId(model.getRoomIds()[i]);
			finishRepair = roomRepairService.finishRepair(model);
		}
		
		if (Utils.isNotNull(finishRepair)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(finishRepair);
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomRepairReturnCodeEnum.ROOMREPAIR_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(RoomRepairReturnCodeEnum.ROOMREPAIR_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomRepairReturnCodeEnum.ROOMREPAIR_UPDATE_FAILURE.getCode());
			result.setReturnMsg(RoomRepairReturnCodeEnum.ROOMREPAIR_UPDATE_FAILURE.getDesc());
		}
		return result;
	}
}
